package ws.jdbc.concurrent.driver.sqlserver.tds;

import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * Handles the {@code RETURNSTATUS} token reading process.
 *
 * <p>The {@code RETURNSTATUS} token stream used to send the status value of an RPC
 * to the client. The server also uses this token to send the result status value
 * of a T-SQL EXEC query.
 * </p>
 *
 * <p>
 * The {@code RETURNSTATUS} token MUST be returned to the client when an RPC stream
 * executed by the server.
 * </p>
 *
 * <p>
 * <a href="http://msdn.microsoft.com/en-us/library/dd358180">Reference</a>
 * </p>
 *
 * @author Wilson Santos, 36228 <wilson.santos@ua.pt>
 */
public class ReturnStatusTokenReader extends TokenReader {

  /**
   * The return status value determined by the remote procedure.
   * Return status MUST NOT be NULL.
   */
  private int value;

  /**
   * Create a new reader instance.
   *
   * @param stream data input stream to read the token from.
   */
  public ReturnStatusTokenReader(final ByteBuffer stream) {
    super(stream);
  }

  /**
   * Read one {@code RETURNSTATUS} token.
   *
   * @return number of bytes read.
   *
   * @throws IOException if an error occurs while reading from the data input stream.
   */
  @Override
  int read() throws IOException {
    value = stream.getInt();
    return 4;
  }

  /**
   * Token as a {@code String}.
   *
   * @return token fields as a {@code String}.
   */
  @Override
  public String toString() {
    return "ReturnStatusTokenReader{"
            + "\n  value=" + value
            + "\n}\n";
  }
}
